﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.DataAnnotations;

namespace SilveR.GUIValidators
{
    public class PValueAdjustmentGUIValidator
    {
        public static ValidationResult CheckStringVarSet(string variable, ValidationContext context)
        {
            return BasicGUIValidator.CheckStringVarSet(variable, context);
        }

        public static ValidationResult CheckPValue(string variable, ValidationContext context)
        {
            if (String.IsNullOrEmpty(variable))
            {
                return new ValidationResult("P values have not been set");
            }
            else //check to ensure that values in list are all numbers and are all comma separated and >0
            {
                string[] pValues = variable.Split(','); //split list by comma

                foreach (string p in pValues)//go through list and check that is a number and is greater than 0
                {
                    double number;
                    if (!double.TryParse(p, out number) && p != "<0.001" && p != "<0.0001") //n.b. remove < sign when checking
                    {
                        return new ValidationResult("P values contains non-numeric values detected or values are not comma separated");
                    }
                    else if (p.Contains(" ")) //check to see if any spaces are included as these are not allowed
                    {
                        return new ValidationResult("Please remove any spaces between the comma separated p-values");
                    }
                    else if (number < 0)
                    {
                        return new ValidationResult("All p-values must be greater than zero");
                    }
                    else if (number > 1)
                    {
                        return new ValidationResult("All p-values must be greater than zero");
                    }
                }

                return ValidationResult.Success;
            }
        }
    }
}
